package com.shawn.jetpackstady.paging;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.paging.PositionalDataSource;

import com.shawn.jetpackstady.paging.bean.Concert;

import java.util.ArrayList;
import java.util.List;

public class ConcertDataSource extends PositionalDataSource {
    /**
     * 加载初始化数据，加载第一页的数据，
     *
     * @param params
     * @param callback
     */
    @Override
    public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {
        Log.i("shawn", "触发第一次初始化数据");
        callback.onResult(fetchItems(0, 20), 0, 1000);
    }

    /**
     * 当有了初始化数据之后，滑动的时候如果需要加载数据的话，会调用此方法
     *
     * @param params
     * @param callback
     */
    @Override
    public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback callback) {
        Log.i("shawn", "触发加载数据");
        Log.i("shawn", "触发加载数据，startPosition = " + params.startPosition);
        Log.i("shawn", "触发加载数据,loadSize = " + params.loadSize);
        callback.onResult(fetchItems(params.startPosition, params.loadSize));
    }

    /**
     * 数据源
     *
     * @param startPosition
     * @param pageSize
     * @return
     */
    private List<Concert> fetchItems(int startPosition, int pageSize) {
        List<Concert> list = new ArrayList<>();
        for (int i = startPosition; i < startPosition + pageSize; i++) {
            Concert concert = new Concert();
            concert.setAuthor("author = " + i);
            concert.setContent("content = " + i);
            concert.setTitle("title = " + i);
            list.add(concert);
        }
        return list;
    }
}
